package com.dlc.felear.xiaoensale.deviceUtil;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.activity.FirstActivity;

public class TopbarHelper {
    private static final String TAG = "TopbarHelper";
    private AppCompatActivity activity;
    private TextView title;
    private ImageView view;
    private Class aClass;
    private boolean returnEnable = true;
    private String key;
    private String value;
    private String msg;

    public void setNavActivity(Class aClass) {
        this.aClass = aClass;
        setReturnEnable(true);
    }

    public TopbarHelper(final AppCompatActivity activity) {
        this.activity = activity;
        view = activity.findViewById(R.id.topbar_iv);
        title = activity.findViewById(R.id.topbar_title);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (returnEnable && aClass != null) {
                    Intent intent = new Intent(activity, aClass);
                    if (!TextUtils.isEmpty(key)) {
                        intent.putExtra(key, value);
                    }
                    activity.startActivity(intent);
                    activity.finish();
                } else {
                    activity.finish();
                    Toast.makeText(activity, TopbarHelper.this.msg, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setNotReturn(String msg) {
        this.msg = msg;

    }

    public void setkey_Value(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public void setReturnEnable(boolean flag) {
        returnEnable = flag;
        if(!flag){
            view.setVisibility(View.INVISIBLE);
        }
    }

    public void setcon(int resId) {
        Glide.with(activity).load(resId).into(view);
    }

    public void setTitle(String msg) {
        if (title != null) {
            title.setText(msg);
        }
    }
}
